package com.weedsdream.app.compose

import androidx.compose.foundation.background
import androidx.compose.foundation.gestures.detectTapGestures
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.BoxScope
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.RowScope
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import coil.compose.AsyncImage
import com.weedsdream.app.R
import com.weedsdream.app.floatWindow.FloatWindowSetUi
import com.weedsdream.app.struct.FileOpe
import com.weedsdream.app.struct.RecordFile
import com.weedsdream.compose.HLine
import com.weedsdream.compose.IconButton
import com.weedsdream.compose.Space
import com.weedsdream.compose.VLine
import com.weedsdream.function.longTime2String

/**历史记录数据*/
val history = mutableStateListOf<RecordFile>()

/**更多Ui开启状态*/
var moreUiOpen by mutableStateOf(false)

/**当前预览的缩略图*/
private var preView by mutableStateOf<RecordFile?>(null)

/**更多内容*/
@Composable
fun BoxScope.More(test: Boolean = true) {
    Space(Color.Black, 0.2f) { moreUiOpen = false }
    Row(Modifier.align(Alignment.TopEnd)) {
        FloatWindowSetUi(test)
        Box(
            Modifier
                .fillMaxHeight()
                .fillMaxWidth(0.4f)
        ) {
            Space()
            Column {
                var saveOnPress by remember { mutableStateOf(false) }
                Box(Modifier
                    .height(40.dp)
                    .fillMaxWidth()
                    .pointerInput(Unit) {
                        detectTapGestures(onPress = {
                            saveOnPress = true
                            tryAwaitRelease()
                            saveOnPress = false
                            FileOpe.saveProject(RecordFile().name)
                            FileOpe.readProjectMenu()
                        })
                    }) {
                    Space(if (saveOnPress) Color.Cyan else Color.Unspecified, 0.3f)
                    Text(text = "保存", modifier = Modifier.align(Alignment.Center))
                }
                HLine()
                //导入导出按钮
                Row(Modifier.height(35.dp)) {
                    ImExPort("导入文件", R.drawable.import_) { FileOpe.fromFile() }
                    VLine()
                    ImExPort("导出当前", R.drawable.export_) { FileOpe.toFile() }
                }
                HLine()
                //历史记录
                LazyColumn(Modifier.fillMaxSize()) { item { history.forEach { it.OneRecord() } } }
            }
        }
    }
    if (test || preView != null) {
        Space(Color.LightGray) { preView = null }
        if (preView != null) AsyncImage(preView!!.thumbnail, "预览缩略图", Modifier.fillMaxSize())
        Box(Modifier
            .height(40.dp)
            .align(Alignment.TopEnd)
            .pointerInput(Unit) {
                detectTapGestures {
                    if (preView != null) {
                        FileOpe.deleteProject(preView!!.name)
                        FileOpe.readProjectMenu()
                        preView = null
                    }
                }
            }) {
            Text(
                "删除此文件", color = Color.Red, fontSize = 15.sp,
                modifier = Modifier
                    .align(Alignment.Center)
                    .padding(10.dp)
            )
        }
        Column(
            Modifier
                .align(Alignment.BottomStart)
                .padding(10.dp)
        ) {
            IconButton(
                iconId = R.drawable.share, iconTint = Color(0xff1299df), tag = "分享",
                tagColor = Color(0xff1299df), tagSize = 12.sp
            ) {
                FileOpe.sharePNG(preView!!)
            }
            Spacer(Modifier.height(10.dp))
            IconButton(
                iconId = R.drawable.download, iconTint = Color.Blue, tag = "保存",
                tagColor = Color.Blue, tagSize = 12.sp
            ) {
                FileOpe.savePNG(preView!!)
            }
        }
    }
}

/**导入导出按钮*/
@Composable
private fun RowScope.ImExPort(title: String, iconId: Int, onclick: () -> Unit) {
    Box(Modifier
        .weight(1f)
        .fillMaxHeight()
        .pointerInput(Unit) {
            detectTapGestures { onclick() }
        }) {
        Column(Modifier.align(Alignment.Center)) {
            Icon(
                painterResource(id = iconId), null, tint = Color.Black,
                modifier = Modifier
                    .size(18.dp)
                    .align(Alignment.CenterHorizontally)
            )
            Text(
                text = title, fontSize = 10.sp,
                modifier = Modifier.align(Alignment.CenterHorizontally)
            )
        }
    }
}

/**一条记录*/
@Composable
private fun RecordFile.OneRecord() {
    val mod = Modifier
        .padding(bottom = 4.dp, start = 3.dp, end = 3.dp)
        .height(35.dp)
        .fillMaxWidth()
        .pointerInput(Unit) { detectTapGestures { FileOpe.readProject(name) } }
    Box(mod) {
        Space(Color.LightGray)
        Row {
            Box(Modifier.fillMaxHeight()) {
                Text(text = name, maxLines = 1, modifier = Modifier.padding(top = 5.dp))
                Text(
                    text = longTime2String(time), fontSize = 10.sp,
                    modifier = Modifier.align(Alignment.BottomStart)
                )
            }
            if (thumbnail != null) {
                val mod1 =
                    Modifier
                        .size(35.dp)
                        .background(Color.White)
                        .align(Alignment.CenterVertically)
                Spacer(Modifier.weight(1f))
                if (thumbnail == "") {
                    Icon(
                        painterResource(com.weedsdream.base.R.drawable.tp_logo), "缩略图示例", mod1,
                        Color.Unspecified
                    )
                } else {
                    AsyncImage(thumbnail, "缩略图", mod1.pointerInput(Unit) {
                        detectTapGestures(onPress = {
                            preView = this@OneRecord
                            tryAwaitRelease()
                            preView = null
                        })
                    })
                }
            }
        }
    }
}

@Preview(widthDp = 600, heightDp = 350)
@Composable
private fun Test() {
    initTestData()
    Box {
        More()
    }
}

private fun initTestData() {
    val a = RecordFile(thumbnail = "")
    for (i in 0..10) history.add(a)
}